<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img1.baidu.com/it/u=969469198,78190688&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >python文件管理</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、字符编码"><a href="#一、字符编码" class="headerlink" title="一、字符编码"></a>一、字符编码</h2><h3 id="1-字符编码"><a href="#1-字符编码" class="headerlink" title="1.字符编码"></a>1.字符编码</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>计算机内部存储数据都是以二进制数字的形式存储</span><br><span class="line">	计算机基于电工作 而电信号只有高低电平两种状态</span><br><span class="line">    也就意味着计算机的世界里只能识别两种状态</span><br><span class="line">    我们人为的将高电平定义为数字<span class="number">1</span></span><br><span class="line">    低电平定义为数字<span class="number">0</span></span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span>单位换算(重要)</span><br><span class="line">	比特位bit(二进制数的个数)</span><br><span class="line">    8bit = 1<span class="built_in">bytes</span>(字节)</span><br><span class="line">    1024<span class="built_in">bytes</span> = 1KB</span><br><span class="line">    1024KB = 1MB</span><br><span class="line">    1024MB = 1GB</span><br><span class="line">    1024GB = 1TB</span><br><span class="line">    1024TB = 1PB</span><br><span class="line">   </span><br><span class="line"><span class="comment"># 字符编码的研究仅限于文本文件(******)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span>为什么我们人类在使用计算机输入字符的时候电脑展示是人类能够看懂的字符</span><br><span class="line">而计算机内部只识别01二进制</span><br><span class="line">	00001010			我</span><br><span class="line">    00001101              你</span><br><span class="line">    计算机二进制			设定好的对应关系		人类的字符</span><br><span class="line">    </span><br><span class="line"><span class="comment"># 字符编码表</span></span><br><span class="line">	规定了人类的字符与数字之间的对应关系</span><br></pre></td></tr></table></figure>

<h3 id="2-字符编码发展史"><a href="#2-字符编码发展史" class="headerlink" title="2.字符编码发展史"></a>2.字符编码发展史</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">一家独大</span><br><span class="line">	计算机是由美国人发明的 美国人需要计算机能够识别英文</span><br><span class="line">    所以发明了ASCII码(字符编码表)</span><br><span class="line">    	记录了英文与数字的对应关系</span><br><span class="line">        	1<span class="built_in">bytes</span>来表示所有的英文及符号</span><br><span class="line">    			8bit	<span class="number">256</span></span><br><span class="line"><span class="comment"># 所有的英文字母及符号加起来其实也不会超过128种可能</span></span><br><span class="line">群雄割据</span><br><span class="line">	中国</span><br><span class="line">    	中国人为了计算机能够识别中文发明了GBK码</span><br><span class="line">        GBK码表</span><br><span class="line">        	记录了中文、英文与数字的对应关系</span><br><span class="line">            	2<span class="built_in">bytes</span>来表示中文及符号</span><br><span class="line">                	16bit       <span class="number">65535</span></span><br><span class="line">                1<span class="built_in">bytes</span>来表示所有的英文及符号</span><br><span class="line"><span class="comment"># 生僻字可能会使用更多的bytes来表示</span></span><br><span class="line">        </span><br><span class="line"> 日本</span><br><span class="line">    日本人为了计算机能识别日文发明了shift_JIS码</span><br><span class="line">        shift_JIS码表</span><br><span class="line">        记录了日文、英文与数字的对应关系</span><br><span class="line">        1<span class="built_in">bytes</span>来表示所有的英文及符号</span><br><span class="line">            	</span><br><span class="line"> 韩国</span><br><span class="line">	韩国人为了计算机能识别韩文发明了Euc_kr码</span><br><span class="line">        Euc_kr码表</span><br><span class="line">        记录了韩文、英文与数字的对应关系</span><br><span class="line">        1<span class="built_in">bytes</span>来表示所有的英文及符号</span><br><span class="line">        </span><br><span class="line">天下一统</span><br><span class="line">    群雄割据导致数据交互不通畅(乱码)</span><br><span class="line">    unicode万国码</span><br><span class="line">    兼容万国字符</span><br><span class="line">    所有的字符同意使用2<span class="built_in">bytes</span>表示 不够增加更<span class="built_in">bytes</span></span><br><span class="line">utf8</span><br><span class="line">    unicode transformation <span class="built_in">format</span></span><br><span class="line">    英文还是用1<span class="built_in">bytes</span></span><br><span class="line">    中文改用3<span class="built_in">bytes</span></span><br><span class="line"><span class="comment"># 内存使用的编码表是unicode 硬盘使用的是utf8</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;已经你们只需要记住文本文件编码统一使用utf8即可&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-字符编码实操"><a href="#3-字符编码实操" class="headerlink" title="3.字符编码实操"></a>3.字符编码实操</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>解决文件乱码的核心</span><br><span class="line">	文件以什么编码存的就以什么编码取</span><br><span class="line"><span class="number">2.</span>编码与解码(重要)</span><br><span class="line">	<span class="comment"># 编码</span></span><br><span class="line">    	将人类的字符按照指定的编码转换成计算机能够识别的数据</span><br><span class="line">        encode()</span><br><span class="line">    <span class="comment"># 解码</span></span><br><span class="line">    	将计算机能否识别的数据按照指定的编码转换成人类能够读懂的字符</span><br><span class="line">        decode()</span><br><span class="line"><span class="number">3.</span>python解释器默认编码</span><br><span class="line">	python2默认的是ASCII码</span><br><span class="line">    	在python2中定义字符串在前面加一个字母u</span><br><span class="line">        文件头指定编码</span><br><span class="line">        	<span class="comment"># -*- conding:utf8 -*-</span></span><br><span class="line">            name = <span class="string">u&#x27;jason&#x27;</span></span><br><span class="line">    python3默认的是utf8码</span><br></pre></td></tr></table></figure>

<h2 id="二、文件操作"><a href="#二、文件操作" class="headerlink" title="二、文件操作"></a>二、文件操作</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">操作系统</span><br><span class="line">	windows mac linux</span><br><span class="line">双击文件</span><br><span class="line">	硬盘转 磁头读取数据</span><br><span class="line">保存文件</span><br><span class="line">	硬盘转 磁头写入数据</span><br><span class="line"> </span><br><span class="line">我们在操作文件的时候其实操作的是硬盘</span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">文件其实是操作系统暴露给我们可以简单快捷的操作硬盘的接口</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 绝对路径与相对路径</span></span><br><span class="line">	绝对路径</span><br><span class="line">    	非常详细的路径描述</span><br><span class="line">        	无论什么人什么位置都可以找到</span><br><span class="line">    相对路径</span><br><span class="line">    	有一个参考</span><br><span class="line">        	只有对应的人才可以找到</span><br><span class="line"></span><br><span class="line">res = <span class="built_in">open</span>(<span class="string">r&#x27;a.txt&#x27;</span>,<span class="string">&#x27;r&#x27;</span>,encoding=<span class="string">&#x27;utf8&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(res.read())</span><br><span class="line">res.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 打开a.txt 使用变量名f指代改文件</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">r&#x27;a.txt&#x27;</span>,<span class="string">&#x27;r&#x27;</span>,encoding=<span class="string">&#x27;utf8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.read())  <span class="comment"># 读取文件内容</span></span><br><span class="line"><span class="comment"># with子代码运行结束之后会自动调用close关闭文件资源</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">with open(文件路径,读写模式,编码格式) as 变量名:</span></span><br><span class="line"><span class="string">    子代码块</span></span><br><span class="line"><span class="string">文件路径是必须的</span></span><br><span class="line"><span class="string">读写模式和编码格式是可选的</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果不指定读写模式 那么默认使用r</span></span><br><span class="line"><span class="comment"># 如果不指定编码格式 那么默认使用当前计算机内部默认编码</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;在中国所有的windows电脑内部默认的编码是gbk&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">r&#x27;a.txt&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.read())  <span class="comment"># 读取文件内容</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 取消转义</span></span><br><span class="line">res = <span class="string">r&#x27;D:\day08\a.txt&#x27;</span></span><br><span class="line">res1 = <span class="string">r&#x27;D:\day08\n.txt&#x27;</span></span><br><span class="line">res2 = <span class="string">r&#x27;D:\day08\t.txt&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(res,res1,res2)</span><br></pre></td></tr></table></figure>

<h2 id="三、文件处理"><a href="#三、文件处理" class="headerlink" title="三、文件处理"></a>三、文件处理</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、控制文件读写操作的模式</span><br><span class="line">r（默认）</span><br><span class="line">w</span><br><span class="line">a</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>、控制文件读写内容的模式</span><br><span class="line">t（默认的）：读写都是以字符串为单位的，只适用于文件文件，必须指定encoding参数</span><br><span class="line">b：读写都是以<span class="built_in">bytes</span>为单位的，适用于所有文件，一定不能指定encoding参数</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">r模式：只读模式，如果文件不存在则报错,文件存在则文件指针处于文件开头</span><br><span class="line"></span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;rt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(f.read())  <span class="comment"># 显示文件内容，显示所有a.txt信息，且光标移动到最后</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;=&#x27;</span>*<span class="number">50</span>)	<span class="comment"># 分隔符号</span></span><br><span class="line"><span class="built_in">print</span>(f.read())  <span class="comment"># 从上面光标位置开始读取内容 ，显示内容为空 </span></span><br><span class="line"><span class="built_in">print</span>(f.readable())  <span class="comment"># True	</span></span><br><span class="line"><span class="built_in">print</span>(f.writable())  <span class="comment"># False</span></span><br><span class="line">f.close()  <span class="comment"># 关闭</span></span><br><span class="line"></span><br><span class="line">w模式：只写模式，如果文件不存在则创建空文档,文件存在则清空，文件指针处于文件开头</span><br><span class="line">简单记忆：w模式下每次都是新建文件将之前的原文件覆盖，必须要备份以免数据丢失</span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;wt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">f.write(<span class="string">&quot;你好\n&quot;</span>)</span><br><span class="line">f.write(<span class="string">&quot;哈哈哈\n&quot;</span>)</span><br><span class="line">f.write(<span class="string">&quot;我擦勒\n&quot;</span>)</span><br><span class="line">f.close</span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;wt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">f.write(<span class="string">&quot;你好\n哈哈哈\n我擦勒\n&quot;</span>)  <span class="comment"># 覆盖a.txt文件，									显示上述内容</span></span><br><span class="line"><span class="built_in">print</span>(f.readable())  <span class="comment"># 可读  False</span></span><br><span class="line"><span class="built_in">print</span>(f.writable())  <span class="comment"># 可写  True</span></span><br><span class="line">f.close()  <span class="comment"># 关闭</span></span><br><span class="line">总结：w模式，在打开了文件不关的情况下，连续的write写入，新写的内容永远跟在后面</span><br><span class="line"></span><br><span class="line">a模式：只追加写模式，如果文件不存在则创建空文档,文件存在不会清空，文件指针处于文件末尾</span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&#x27;c.txt&#x27;</span>,mode=<span class="string">&#x27;at&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">f.write(<span class="string">&quot;4444\n&quot;</span>)</span><br><span class="line">f.write(<span class="string">&quot;555\n&quot;</span>)</span><br><span class="line">f.write(<span class="string">&quot;6666\n&quot;</span>)  <span class="comment"># a模式永远写在后面</span></span><br><span class="line"><span class="built_in">print</span>(f.readable())  <span class="comment"># False</span></span><br><span class="line"><span class="built_in">print</span>(f.writable())  <span class="comment"># True</span></span><br><span class="line">f.close()  <span class="comment"># 关闭</span></span><br><span class="line"></span><br><span class="line">总结：a模式，在打开了文件不关的情况下，连续的write写入，新写的内容永远跟在后面,这一点与w模式相同</span><br><span class="line">   不同的是，在打开了文件关闭然后重新打开的情况下，a模式永远写在后面</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">b模式：读写都是以<span class="built_in">bytes</span>为单位的，适用于所有文件，一定不能指定encoding参数</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;1.mp4&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f:  <span class="comment"># 视频格式</span></span><br><span class="line">    res = f.read()</span><br><span class="line">    <span class="built_in">print</span>(res.decode(<span class="string">&#x27;utf-8&#x27;</span>))  <span class="comment"># 报错 不能解码</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f:  <span class="comment"># 文本文件</span></span><br><span class="line">    res = f.read()</span><br><span class="line">    <span class="built_in">print</span>(res.decode(<span class="string">&#x27;utf-8&#x27;</span>))  <span class="comment"># 文本文件 可以解码读取内容</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;ab&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    f.write(<span class="string">&quot;啊手动阀手动阀&quot;</span>.encode(<span class="string">&#x27;utf-8&#x27;</span>))  <span class="comment"># 直接在文件最后追加</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">例：文件拷贝功能</span><br><span class="line"><span class="comment"># 方案一: 整体复制，缺陷：文件过大内存可能会存在溢出</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;1.mp4&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f1,<span class="built_in">open</span>(<span class="string">r&#x27;D:\1111111.mp4&#x27;</span>,mode=<span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> f2:</span><br><span class="line">    res = f1.read()</span><br><span class="line">    f2.write(res)</span><br><span class="line">    f2.write(f1.read)  <span class="comment">#复制文件至指定位置</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案二： </span></span><br><span class="line">储备知识: </span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;rt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="comment"># f.read()</span></span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">        <span class="built_in">print</span>(line,end=<span class="string">&#x27;&#x27;</span>) <span class="comment"># end&#x27;&#x27;可以删除\n 不加的话每行之间会有空格</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;1.mp4&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">        <span class="built_in">print</span>(line)  <span class="comment"># 解码后跳出二进制码</span></span><br><span class="line"></span><br><span class="line">具体实现：</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;1.mp4&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f1,<span class="built_in">open</span>(<span class="string">r&#x27;D:\1111111.mp4&#x27;</span>,mode=<span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> f2:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f1:  <span class="comment"># 将1.mp4转码后进行分行复制</span></span><br><span class="line">        f2.write(line)  <span class="comment"># 将分行后逐条内容复制到f2指定的位置</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">可读可写  <span class="comment"># 如下命令不怎么使用</span></span><br><span class="line">r+t</span><br><span class="line">w+t</span><br><span class="line">a+t</span><br><span class="line"></span><br><span class="line">r+b</span><br><span class="line">w+b</span><br><span class="line">a+b</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;r+t&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.readable())  <span class="comment"># True</span></span><br><span class="line">    <span class="built_in">print</span>(f.writable())  <span class="comment"># True</span></span><br><span class="line">    <span class="built_in">print</span>(f.read())  </span><br><span class="line">    f.write(<span class="string">&quot;22222222222222222222\n&quot;</span>)  <span class="comment"># 在文件最后添加新增的内容</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;w+t&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.readable())   <span class="comment"># True</span></span><br><span class="line">    <span class="built_in">print</span>(f.writable())   <span class="comment"># True</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="1、控制文件指针移动"><a href="#1、控制文件指针移动" class="headerlink" title="1、控制文件指针移动"></a>1、控制文件指针移动</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># f.seek移动的全都是字节个数</span></span><br><span class="line"><span class="comment"># f.seek(字节个数,0)  # 文件最开始的地方</span></span><br><span class="line"><span class="comment"># f.seek(字节个数,1)  # 指针移动到的地方</span></span><br><span class="line"><span class="comment"># f.seek(字节个数,2)  # 文件末尾</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ps：只有0模式可以t下使用，其中1和2模式只能在b模式下使用，但是无论在t模式还是b模式下，移动的都是字节个数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 例如:a.txt 内容：hello你好kasjdkla</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">r&quot;a.txt&quot;</span>,mode=<span class="string">&quot;rt&quot;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.tell()) <span class="comment"># 0  查看当前文件指针距离文件开头的位置，输出结果为0</span></span><br><span class="line">    f.seek(<span class="number">9</span>,<span class="number">0</span>)  <span class="comment"># 9 查看当前文件指针距离文件开头的位置，输出结果为9</span></span><br><span class="line">    f.seek(<span class="number">3</span>,<span class="number">0</span>)  <span class="comment"># 3 查看当前文件指针距离文件开头的位置，输出结果为3</span></span><br><span class="line">    <span class="built_in">print</span>(f.tell())  <span class="comment"># 3 查看当前文件指针距离文件开头的位置，最终输出结果为3</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.tell())  <span class="comment">#0  从指针当前位置往后移动0个字节，而此时的当前位置就是文件开头</span></span><br><span class="line">    f.seek(<span class="number">9</span>,<span class="number">1</span>)  <span class="comment"># 9 从当前位置往后移动9个字节，而此时的当前位置就是文件开头</span></span><br><span class="line">    f.seek(<span class="number">3</span>,<span class="number">1</span>)  <span class="comment"># 12 从当前位置往后移动3个字节，而此时的当前位置9</span></span><br><span class="line">    <span class="built_in">print</span>(f.tell())  <span class="comment"># 12 从当前位置往后移动3个字节，而此时的当前位置9</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.tell())  <span class="comment">#0 从指针当前位置往后移动0个字节，而此时的当前位置就是文件开头</span></span><br><span class="line">    f.seek(<span class="number">0</span>,<span class="number">2</span>)  <span class="comment"># 参照文件末尾移动19个字节， 即直接跳到文件末尾</span></span><br><span class="line">    <span class="built_in">print</span>(f.tell())  <span class="comment"># 19 输出结果为：19</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>,mode=<span class="string">&#x27;ab&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="built_in">print</span>(f.tell())  <span class="comment"># 19 追加模式指针移动到最后，显示全部字节</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Linux命令：tail -f access.log</span></span><br></pre></td></tr></table></figure>

<h3 id="2、文件修改的两种方式"><a href="#2、文件修改的两种方式" class="headerlink" title="2、文件修改的两种方式"></a>2、文件修改的两种方式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 例如:a.txt 内容：hello你好kasjdkla</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>, mode=<span class="string">&#x27;r+t&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">	f.seek(<span class="number">9</span>,<span class="number">0</span>)</span><br><span class="line">	f.write(<span class="string">&quot;hello&quot;</span>)  <span class="comment">#hello你�hellojdkla </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># PS:文件修改不是修改，而是执行覆盖命令</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">文件修改的原理：</span></span><br><span class="line"><span class="string">把硬盘数据读入内存，在内存修改完毕后，再覆盖回硬盘</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="comment"># 具体来说又分为两种方案</span></span><br><span class="line"><span class="comment"># 方案一：  利：不占硬盘空间 弊：占据内存空间</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;test.txt&#x27;</span>,mode=<span class="string">&#x27;rt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">     data = f.read()</span><br><span class="line"></span><br><span class="line"> <span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;test.txt&#x27;</span>,mode=<span class="string">&#x27;wt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">     f.write(data.replace(<span class="string">&#x27;egon&#x27;</span>,<span class="string">&#x27;EGON&#x27;</span>))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案二：  利：不占内存空间 弊：占据硬盘空间（2倍）</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;test.txt&#x27;</span>,mode=<span class="string">&#x27;rt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f1,\</span><br><span class="line">        <span class="built_in">open</span>(<span class="string">&#x27;.test.txt.swp&#x27;</span>,mode=<span class="string">&#x27;wt&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f2:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f1:</span><br><span class="line">        f2.write(line.replace(<span class="string">&#x27;EGON&#x27;</span>,<span class="string">&quot;egon&quot;</span>))</span><br><span class="line"></span><br><span class="line">os.remove(<span class="string">&#x27;test.txt&#x27;</span>)</span><br><span class="line">os.rename(<span class="string">&#x27;.test.txt.swp&#x27;</span>,<span class="string">&#x27;test.txt&#x27;</span>)</span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/28/Python------%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81&amp;%E6%96%87%E4%BB%B6%E7%AE%A1%E7%90%86/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/28/Python------%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81&amp;%E6%96%87%E4%BB%B6%E7%AE%A1%E7%90%86/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
